package cn.hxzy.course.controller;

import cn.hxzy.common.domain.Courseware;
import cn.hxzy.common.domain.FileInfo;
import cn.hxzy.common.response.R;
import cn.hxzy.common.response.StatusEnum;
import cn.hxzy.course.service.FileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/FileInfo")
public class FileInfoController {
    @Autowired
    private FileInfoService fileInfoService;

    @PostMapping("/saveFile")
    public R saveFile(@RequestBody FileInfo fileInfo) {
        int i = fileInfoService.insertFileInfo(fileInfo);
        if (i>0){
            return R.success(i);
        }
        return R.error(StatusEnum.PARAM_INVALID);
    }


    /**
     * 搜索框搜索资料
     * @return
     */
    @GetMapping("/selectFileInfo/{infoName}/{courseId}")
    public R<List<FileInfo>> selectWare(@PathVariable("infoName") String infoName,@PathVariable("courseId") Integer courseId) {
        List<FileInfo> fileInfos = fileInfoService.selectFileInfo(infoName,courseId);
        return R.success(fileInfos);
    }

    @GetMapping("/getFileInfoNum")
    public R getVideoNum(@RequestParam("courseId") Long courseId) {
        return R.success(fileInfoService.selectCount(courseId));
    }

    @GetMapping("/getFileInfo")
    public R selectWareList(@RequestParam("sectionId") Integer sectionId) {

        List<FileInfo> fileInfos = fileInfoService.selectFileInfoNum(sectionId);
        if (fileInfos != null){
            return R.success(fileInfos);
        }
        return R.error(401,"查询失败");
    }

    //删除资料
    @DeleteMapping("/deleteFileInfo")
    public R deleteFileInfo(@RequestParam("infoId") Integer infoId) {
        int i = fileInfoService.deleteFileInfo(infoId);
        if (i>0){
            return R.success("删除成功");
        }
        return R.error(401,"删除失败");
    }
}
